// 获取当前经纬度
var lng = 0,
  lat = 0; //经纬度
if (window.navigator.geolocation) {
  var options = {
    enableHighAccuracy: true,
  };
  window.navigator.geolocation.getCurrentPosition(handleSuccess, handleError, options);
} else {
  //浏览器不支持html5来获取地理位置信息
}
Auth();

function handleSuccess(position) {
  // 获取到当前位置经纬度  本例中是chrome浏览器取到的是google地图中的经纬度
  lng = position.coords.longitude;
  lat = position.coords.latitude;
  console.log('lng：' + lng + '========lat：' + lat);
}


function handleError(error) { //地理位置信息获取失败
  console.log('地理位置信息获取失败');
}
var fwId = globalFun.GetQueryString("id"),
  longitude = globalFun.GetQueryString("longitude"),
  latitude = globalFun.GetQueryString("latitude"),memid;
fwId = fwId ? fwId : 2;
longitude = longitude ? longitude : 111;
latitude = latitude ? latitude : 12;
var topicId = globalFun.GetQueryString("topicId");
var shareId = globalFun.GetQueryString("share");
var fromApp = globalFun.GetQueryString("from");
var merchant_id, isFollow,
  fwType, fwImg, starttime, endtime, dataMsg = {},
  shareMsg = {};
shareMsg.url = location.href;
$('.nav1-img').click(function() {
  if (!fromApp) {
    window.history.back(-1);
  } else {
    if (mark === 1) {
      setupWebViewJavascriptBridge(function(bridge) {
        bridge.callHandler('goBack', function(resp) {});
      });
    } else if (mark === 0) {
      //var result = lase;
      window.stub.goBack(); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

    }
  }
});
$('.sppj-4').click(function() {
  var src = $("#mySwipe .swipe-wrap div:first-child img").attr("src")
  sessionStorage.setItem('da', src);
  localStorage.setItem("shareMsg", shareMsg);
  var type = $(this).attr("type");
  if ($(".head-js").html()) {
    var ll = $(".head-js").html()
  } else {
    var ll = 'lllll';
  }
  var dateobj = {
    //			serviceProjectId:fwId,

    //			uuid:user_info.uuid,
    //			phone:user_info.phone,
    url: location.href + '&share=' + user_info.id,
    type: 1,
    title: $(".head-mc").html(),
    content: ll,
    //			memberId:user_info.id	
  }
  sessionStorage.setItem("fwxm", JSON.stringify(dateobj));
  window.location.href = "homeshoppj.html?id=" + fwId + "&type=" + type;

});
$(".sppj-z p").click(function() {
  var type = $(this).attr("type");
  localStorage.setItem("shareMsg", shareMsg);
  window.location.href = "homeshoppj.html?id=" + fwId + "&type=" + type;
});
$('.foot-b').click(function() {
  if (!Auth()) {
    toClientLogin();
  } else {
    var fwMsg = {
      title: $(".head-mc").html(),
      type: fwType,
      imgUrl: fwImg,
      price: $(".head-rem").html(),
      starttime: starttime,
      endtime: endtime,
      fwId: fwId,
      id: user_info.id,
      topicId: topicId,
      shareMemberId: shareId
    }
    sessionStorage.setItem("fwMsg", JSON.stringify(fwMsg));
    window.location.href = "../service/s_order.html";
  }
});
$(".aaa").click(function() {
  window.location.href = "../shopDetail/shopDetail.html";
});
// 客服
function kefu(dataObj) {
  if (mark === 1) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsCustomerService', dataObj, function(resp) {});
    });
  } else if (mark === 0) {
    //var result = lase;
    dataObj = JSON.stringify(dataObj);
    window.stub.jsCustomerService(dataObj); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

  }
}

$('.foot-a').click(function() {
  //			var $self = $(this);
  //			if($self.hasClass("disabled")){
  //				return false
  //			}
  //			$self.addClass("disabled")
  //			setTimeout(function(){
  //				$self.removeClass("disabled")
  //			},1000)
  //	
  //			if(Auth()){
  //				var dataObj = {
  //					nc:nc,
  //					img:img,
  //					uuid:imId
  //				}
  //				var dataObjT = JSON.stringify(dataObj);
  //				kefu(dataObjT);
  //			} else{
  //				sessionStorage.setItem('history', window.location.href);
  //				toClientLogin(location.href);
  //			}
  if (!Auth()) {
    toClientLogin();
  } else {
		if(memid == user_info.id){
			tips("不能与自己聊天");
		}else{
			kefu(dataMsg);
		}
    
  }
});

//分享
function zhuanfa(data) {
  if (mark === 1) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsToClientShare', data, function(resp) {});
    });
  } else if (mark === 0) {
    //var result = lase;
    var shareData = JSON.stringify(data);
    window.stub.jsToClientShare(shareData); //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

  }
}
$('.navDp-img').on('click', function() {
  //判断是否登陆
  //		if(Auth()){
  //		var data = {
  //			uuid:user_info.uuid,
  //			phone:user_info.phone,
  //			memberId:user_info.id
  //		}
  //		console.log(user_info.id)
  //		$.ajax({
  //			type:"post",
  //		 	url:apicom + "mobile/after/member/memberdetail",
  //		 	data:data,
  //		 	async: false,
  //			dataType: "json",
  //			success:function(res){
  //				if(typeof(res) == 'string'){
  //					res = JSON.parse(res);
  //				}
  //				if(res.content.isHunter == 1){
  //					isHunter = user_info.id
  //
  //				} else{
  //					isHunter = ''
  //				}
  //			},
  //			error:function(err){
  //				console.log(err)
  //			}
  //		})
  //	} else{
  //		sessionStorage.setItem('history', window.location.href);
  //		toClientLogin(location.href)
  //	}

  //		var $self = $(this)
  //		if($self.hasClass("disabled")){
  //			return false
  //		}
  //		$self.addClass("disabled")
  //		setTimeout(function(){
  //			$self.removeClass("disabled")
  //		},1000)

  //		var data = {
  //			type:1,
  //			url:rootdomain +'html/shopCart/shopDetail.html?type=1&id='+id+'&hunter_id='+isHunter+'&merchant_id='+merchant,
  //			title:'拉阿拉啦啦啦啦啦',
  //			content:'是啊a阿发啊发啊啊啊sk；啊',
  //			hunterId:hunterId
  //		}
  //		var share = JSON.stringify(data);
  if (!Auth()) {
    toClientLogin();
  } else {
    zhuanfa(shareMsg);
  }
});
$.ajax({
  type: "post",
  url: apicom + "mobile/view/serviceProject/getServiceProjectDetail",
  data: {
    serviceProjectId: fwId,
    longitude: longitude,
    latitude: latitude,
    id: (user_info.id ? user_info.id : "")
  },
  success: function(res) {
    if (typeof res == "string") {
      var data = JSON.parse(res);
    } else {
      var data = res;
    }
    var xqContent = '';
    if (data.code == 1) {

      data = data.content;
      //      	分享信息
      shareMsg.title = data.title;
      shareMsg.type = 1;
      shareMsg.content = data.introduce;
      shareMsg.image_address = data.image_address;
      fwType = data.type;
      //    fwImg = data.detailImgeList[0].imageAddress;
      fwImg = data.image_address;
      endtime = data.service_end_time;
      starttime = data.service_start_time;
      merchant_id = data.merchant_id;
      if (data.isFollow) {
        //1 已收藏   0 未收藏
        isFollow = data.isFollow;
        if (isFollow == 0) {
          //      		$(".navFx-img img").attr("src","../../img/d_xin.png");
          $(".navFx-img").removeClass("red");
        } else {
          //      		$(".navFx-img img").attr("src","../../img/d_xin.png");
          $(".navFx-img").addClass("red");
        }
      } else {

      }
      $(".sppj-zsl").html(data.allCommentNum);
      $(".hp").html(data.goodCommentNum);
      $(".zp").html(data.secondCommentNum);
      $(".cp").html(data.badCommentNum);
      $(".head-mc").html(data.title);
      $(".head-rem").html(data.price);

      $(".head-js").html(data.introduce);

      $("#limitTime").html('<span class="dian">.</span>' + data.service_start_time + '至 ' + data.service_end_time);
      var lunbo = '';
      $.each(data.detailImgeList, function(index, val) {
        //      		if(index == 0){
        if (val.label == null) {
          lunbo += '<div><a href="javascript:;"><img class="img-responsive" src="' + val.imageAddress + '"/></a></div>';
        } else {
          lunbo += '<div>';
          var label = JSON.parse(val.label);
          $.each(label, function(index, val) {
            if ((val.productX) * 1 > 50) {
              lunbo += '<b class="shop" productId="' + val.productId + '" style="top:' + val.productY + '%;right:' + val.productX + '%;" productId="' + val.productId + '"><s class="right"><i></i></s><span class="right">' + val.productName + '</span></b>';
            } else {
              lunbo += '<b class="shop" productId="' + val.productId + '" style="top:' + val.productY + '%;left:' + val.productX + '%;" productId="' + val.productId + '"><s><i></i></s><span>' + val.productName + '</span></b>';
            }
          });
          lunbo += '<a href="javascript:;"><img class="img-responsive" src="' + val.imageAddress + '"/></a></div>';
        }
        //      		}else{
        //      			lunbo+='<div><a href="javascript:;"><img class="img-responsive" src="'+val.imageAddress+'"/></a></div>'; 	
        //      		}

      })
      $('.swipe-wrap').html(lunbo);
      $('.swipe-wrap1').html(lunbo);
      $('#lunbo-zs').html(data.detailImgeList.length);
      //小轮播图初始化
      var elem = document.getElementById('mySwipe');
      window.mySwipe = Swipe(elem, {
        //4s一次轮播
        effect: 4000,
        type: 'bullets',
        disableScroll: false,
        callback: function(index, element) {
          $("#position li").eq(index).addClass("active").siblings().removeClass("active");
          $('#current-in').html(index + 1)
          console.log(index)
        }
      });
      $("#position li").click(
        function() {
          mySwipe.slide($(this).index());
        });


      //评论信息
      //      	var commentMsg=data.lastCommentMap.lastComment;
      //是否匿名
      if (data.lastCommentMap == null || data.lastCommentMap.lastComment == null) {
        $(".sppj-2").html("");
        $(".sppj-3").html("");
      } else {
        var commentMsg = data.lastCommentMap.lastComment;
        if (commentMsg.anonymity == 1) {
          commentMsg.memberName = commentMsg.memberName;
        } else {
          if (commentMsg.memberName.length > 2) {
            var xxStr = '';
            for (var i = 0; i < commentMsg.memberName.length - 2; i++) {
              xxStr += "*";
            }
            commentMsg.memberName = commentMsg.memberName.substr(0, 1) + xxStr + commentMsg.memberName.substr(commentMsg.memberName.length - 1, commentMsg.memberName.length);


          } else if (commentMsg.memberName.length == 2) {
            commentMsg.memberName = "**";
          } else if (commentMsg.memberName.length == 1) {
            commentMsg.memberName = "*";
          }
        }
        commentMsg.logo=commentMsg.logo?commentMsg.logo:"../../img/b_moren3x.png";
        $(".sppj2-img img").attr("src", commentMsg.logo);
        $(".sppname").html(commentMsg.memberName);
        $(".spptime").html(commentMsg.createdTime);
        $(".sppj-3 p").html(commentMsg.content);
        var xxContent = '';
        for (var i = 0; i < commentMsg.startNum; i++) {
          xxContent += '<img src="../../img/d_staryou.png">';
        }
        for (var i = 0; i < (5 - commentMsg.startNum); i++) {
          xxContent += '<img src="../../img/d_starwu.png">';
        }
        $(".xxnum").html(xxContent);
      }


      //团队信息
      var teamMsg = data.teamMap;
      memid=teamMsg.member_id;
      dataMsg.uuid = teamMsg.imId;
      dataMsg.imgUrl = teamMsg.imgUrl;
      dataMsg.nikeName = teamMsg.nikeName;
      $(".teamInformation-qmb").html(teamMsg.distance);
      $(".teamInformation-dz").html(teamMsg.provinceName + teamMsg.cityName + teamMsg.countyName);
      $(".teamInformation-mc").html(teamMsg.nikeName);
      $(".teamInformation_b").attr("fwId", teamMsg.id);
      $(".teamInformationb-Right").attr("phone", teamMsg.phone);
      //详情
      var xqMsg = data.ImgeAndWordList;
      $.each(xqMsg, function(index, val) {
        if (val.type == 0) {
          xqContent += '<p class="details-zwjs" style="word-break:break-all;">' + val.detailContent + '</p>';
        } else {
          //xqContent += '<p class="details-zwjs">' + val.detailContent + '</p><div class="details-pic"><img src="' + val.imageAddress + '"/></div>'
          xqContent += '<p class="details-zwjs"></p><div class="details-pic"><img src="' + val.detailContent + '"/></div>'
        }
      });
      $(".details_b").html(xqContent);
    }
    if (data.code == 0) {
      tips(data.msg);
    }
  },
  error: function(err) {
    tips("服务器错误!");
  }
});

/* 联系服务团队 */
$(".teamInformationb-Right").click(function() {
  var phone = $(this).attr("phone");
  if (phone == false) {
    returnfoot-zxtd
  }
  if (mark == 1) {
    setupWebViewJavascriptBridge(function(bridge) {
      bridge.callHandler('jsToClientCall', phone, function(resp) {});
    });
  } else if (mark == 0) {
    //var result = lase;
    window.stub.jsToClientCall(phone); //用接口stub, 通过调用内部类中的方法jsMethod给java传回result。

  }
})
//收藏服务
$('.navFx-img').click(function() {
  var $this = $(this);
  if (!Auth()) {
    toClientLogin();
  } else {
    if ($(this).hasClass("red")) {
      $.ajax({
        type: "post",
        url: apicom + "mobile/after/collection/collectionServiceProject",
        data: {
          serviceProjectId: fwId,
          uuid: user_info.uuid,
          phone: user_info.phone,
          type: 11,
          memberId: user_info.id
        },
        success: function(res) {
          if (typeof res == "string") {
            var data = JSON.parse(res);
          } else {
            var data = res;
          }
          if (data.code == 1) {
            $this.removeClass("red")
            tips("取消收藏");
          }
          if (data.code == 0) {
            tips(data.msg);
          }
        },
        error: function(err) {
          tips("服务器错误!");
        }
      });
    } else {
      $.ajax({
        type: "post",
        url: apicom + "mobile/after/collection/collectionServiceProject",
        data: {
          serviceProjectId: fwId,
          uuid: user_info.uuid,
          phone: user_info.phone,
          type: 10,
          memberId: user_info.id
        },
        success: function(res) {
          if (typeof res == "string") {
            var data = JSON.parse(res);
          } else {
            var data = res;
          }
          if (data.code == 1) {
            $this.addClass("red")
            tips("收藏成功");
          }
          if (data.code == 0) {
            tips(data.msg);
          }
        },
        error: function(err) {
          tips("服务器错误!");
        }
      });
    }
  }

});



//打电话
$(".teamInformationb-Right").click(function() {
  var tele = $(this).attr("phone");
  location.href = 'tel://' + tele;
});


$(document).on("click", ".shop", function(e) {
  var hid = globalFun.GetQueryString("share")
  var productId = $(this).attr("productId");
  e.preventDefault();
  if(hid==null){
       location.href = "../shopDetail/shopDetail.html?id=" + productId + "&topicId=" + topicId;
  }else{  
      location.href = "../shopDetail/shopDetail.html?id=" + productId + "&topicId=" + topicId+ "&share=" +hid;    
  }
  
 
});

$(document).on("click",".swipe-wrap div",function(e){
	e.stopPropagation();
	$('.liehome').addClass("bigLb");
});

// 点击放小
$('.liehome').on('click', function() {
  $(this).removeClass("bigLb");
})


$('.wenzi').click(function() {
  window.location.href = "../shopDetail/shopDetail.html";
});



/* 分享回调 */
function shareCallBack(data1) {
  var dataObj = {
    type: 4,
    id: data1,
    phone: user_info.phone,
    uuid: user_info.uuid
  }
  $.ajax({
    url: apicom + "mobile/after/share/share",
    type: 'POST',
    dataType: 'json',
    data: dataObj,
    success: function(result) {
      var res;
      if (typeof(result) == "string") {
        res = JSON.parse(result);
      } else {
        res = result
      }
      if (res.code == 1) {

        //需要进行的操作
      }
    },
    error: function() {
      //提示
      layer.open({
        content: '服务器连接失败',
        skin: 'msg',
        time: 2 //2秒后自动关闭
      });
    }
  })
}


if (mark == 1) {
  setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('clientToJsShare', function(data, resp) {

      shareCallBack(fwId)
    })
  })
} else if (mark == 0) {
  function clientToJsShare() {

    shareCallBack(fwId)
  }
}